<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Beans.Requests.DataRetrieval.MiscDataRetrievalRequest"%>
<%@page import="Beans.Responses.DataRetrievalComponents.MiscDataComponent"%>
<%@page import="Beans.Requests.DataRetrieval.NetworksDataRetrievalRequest"%>
<%@page import="Beans.Responses.DataRetrievalComponents.NetworkDataComponent"%>
<%@page import="Utilities.Functions"%>
<%@page import="Utilities.UIFunctions"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Utilities.Constants"%>
<%@page import="Beans.Responses.DataRetrievalResponse"%>
<%@page import="Managers.Utilities.User"%>
<%@page import="Beans.Responses.DataRetrievalComponents.MachineDataComponent"%>
<%@page import="Beans.Requests.DataRetrieval.MachinesDataRetrievalRequest"%>
<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@taglib uri="/WEB-INF/tlds/pagePrintingTags.tld" prefix="p"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstlc"%>
<%
    //retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);

    //data retrieval requests
    MachinesDataRetrievalRequest dataRequest = null;
    MiscDataRetrievalRequest mediumFormatsRequest = null;
    
    //data retrieval responses
    DataRetrievalResponse<MachineDataComponent> data = null;
    DataRetrievalResponse<MiscDataComponent> mediumFormatsData = null;

    //retrieves a machine UUID if supplied
    String machineId = request.getParameter("id");

    //attempts to retrieve an owner ID (for limiting the retrieval)
    int ownerId = Constants.INVALID_USER_ID;
    try
    {
        ownerId = Integer.parseInt(request.getParameter("ownerId"));
    }
    catch(NumberFormatException e)
    {}

    //based on the supplied arguments, the data request is built
    if(Functions.isUUIDValid(machineId))
    {
        //a signe machine is to be retrieved
        dataRequest = new MachinesDataRetrievalRequest(userSessionData.getUserData().getUserId(), 
                                                       userSessionData.getCurrentServerId(),
                                                       machineId);

        mediumFormatsRequest = new MiscDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                            userSessionData.getCurrentServerId(),
                                                            Constants.MiscellaneousRequestType.MEDIUM_FORMATS);
    }
    else if(Functions.isUserIdValid(ownerId))
    {
        //all machines owned by the spcified user are to be retrieved
        dataRequest = new MachinesDataRetrievalRequest(userSessionData.getUserData().getUserId(), 
                                                       userSessionData.getCurrentServerId(),
                                                       ownerId);
    }
    else
    {
        //all available machines are to be retrieved
        dataRequest = new MachinesDataRetrievalRequest(userSessionData.getUserData().getUserId(),
                                                       userSessionData.getCurrentServerId());
    }

    //attempts to submit the request(s) and retrieves the responses
    if(UIFunctions.sendServiceRequest(request, response, dataRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
    {
        data = (DataRetrievalResponse<MachineDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);

        if(UIFunctions.sendServiceRequest(request, response, mediumFormatsRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
            mediumFormatsData = (DataRetrievalResponse<MiscDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);
        else
            ;
    }
    else
        ;
%>
<p:printPageHeader pageName="Machines Management" pageId="pageMachines" userSessionData="<%= userSessionData %>"/>
    <p:printMachines data="<%= data %>" mediumFormatsData="<%= mediumFormatsData %>" />
<p:printPageFooter/>